package ui.popup 
{
	import com.GCShip;
	import com.Logging.ClientAction;
	import com.Logging.ClientActionType;
	import data.GCBuildingData;
	import data.GCShipData;
	import events.GCEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import ui.button.GCButton;
	import ui.list.GCItemScrollList;
	import ui.list.GCShipScrollList;
	import ui.list.ScrollList;
	import ui.textfield.GCTextField;
	/**
	 * ...
	 * @author Sterling Swigart
	 */
	public class GCChangeShipPopup extends Sprite
	{
		public static const WIDTH:uint = 500;
		public static const HEIGHT:uint = 500;
		public static const SIDES_MARGIN:uint = 30;
		public static const MIDDLE_COL_MARGIN:uint = 30;
		public static const BOXES_TO_BUTTONS_MARGIN:uint = 30;
		public static const BUTTON_WIDTH:uint = 100;
		public static const BUTTON_HEIGHT:uint = 30;
		
		private var _shipInFocus:GCShip;
		
		private var _shipsList:GCShipScrollList;
		private var _descriptionBox:Sprite;
		private var _descriptionText:TextField;
		
		private var _changeButton:GCButton;
		private var _cancelButton:GCButton;
		
		public function GCChangeShipPopup() 
		{
			// The list of ships
			_shipsList = new GCShipScrollList((WIDTH / 2) - SIDES_MARGIN - (MIDDLE_COL_MARGIN / 2),
					HEIGHT - (2 * SIDES_MARGIN) - MIDDLE_COL_MARGIN - BUTTON_HEIGHT,
					GCPlayState.playerData._playerAllShips);
			_shipsList.x = SIDES_MARGIN;
			_shipsList.y = SIDES_MARGIN;
			_shipsList.addEventListener(GCEvent.LIST_ITEM_SELECTED_EVENT, onShipSelect);			
			addChild(_shipsList);
			
			// Ship Description
			_descriptionBox = new Sprite();
			_descriptionBox.x = (WIDTH / 2) + (MIDDLE_COL_MARGIN / 2);
			_descriptionBox.y = SIDES_MARGIN;
			_descriptionBox.graphics.beginFill(0x222222);
			_descriptionBox.graphics.drawRect(0, 0, 300, 400);
			_descriptionBox.graphics.endFill();
			addChild(_descriptionBox);
			
			_descriptionText = new TextField();
			_descriptionText.textColor = 0xFFFFFF;
			_descriptionText.text = "";
			_descriptionText.width = _descriptionBox.width;
			_descriptionText.height = _descriptionBox.height;
			_descriptionText.wordWrap = true;
			_descriptionBox.addChild(_descriptionText);
			
			// Buttons
			_changeButton = new GCButton(onChange);
			_changeButton.loadGraphic(GCResources.plate, BUTTON_WIDTH, BUTTON_HEIGHT);
			_changeButton.loadText("Change");
			_changeButton.x = (WIDTH / 4) - (BUTTON_WIDTH / 2);
			_changeButton.y = HEIGHT - SIDES_MARGIN - BUTTON_HEIGHT;
			addChild(_changeButton); // TODO: Need to load graphic after addChild???
			_changeButton.disable(); // Disable by default (no ship selected).
			
			_cancelButton = new GCButton(onCancel);
			_cancelButton.loadGraphic(GCResources.plate, BUTTON_WIDTH, BUTTON_HEIGHT);
			_cancelButton.loadText("Cancel");
			_cancelButton.x = (WIDTH * 3 / 4) - (BUTTON_WIDTH / 2);
			_cancelButton.y = HEIGHT - SIDES_MARGIN - BUTTON_HEIGHT;
			addChild(_cancelButton);
			
			addChildAt(GCResources.getMaskedCanvasBackground(this.width + 50, this.height + 50), 0);
		}	
		
		public function onShipSelect(event:GCEvent = null):void
		{
			_shipInFocus = _shipsList.selectedShip.reference;
			var shipId:uint = _shipInFocus.getType();
			
			if (_shipInFocus == GCPlayState.getCurrentShip()) {
				_changeButton.disable();
			} else {
				_changeButton.enable();
			}
			
			_descriptionText.text = GCShipData.getNameFromId(shipId) + "\n\n"
					+ GCShipData.getDescriptionFromId(shipId) + "\n\n";
			var stats:Vector.<uint> = _shipInFocus._stats; //GCShipData.getStatsFromId(shipId);
			// Roy- removing fuel and maintenance
			for (var i:uint = 0; i < stats.length - 2; i++) {
				_descriptionText.appendText(GCShipData.getStatName(i) + ": " + stats[i] + "\n");
			}
		}
		
		public function onChange(event:MouseEvent):void
		{
			if (GCConstants.LOGGING_DATA)
			{
				var o:Object = new Object();
				o["fromType"] = GCPlayState.playerData.currentShip;
				o["toType"] = _shipInFocus.getType();
				GCPlayState.logger.LogAction(new ClientAction(ClientActionType.CHANGE_ACTIVE, o));
			}
			
			if (!GCConstants.DEBUG_MODE)
				GCUtil.changeActiveShip(GCPlayState.getPlayerId(), _shipInFocus.shipId, function (code:uint, data:Object):void{});
			GCPlayState.changeActiveShip(_shipInFocus);
			GCPlayState.enableScreen();
			dispatchEvent(new Event(GCEvent.CHANGE_ACTIVE_SHIP_EVENT));
		}
		
		public function onCancel(event:MouseEvent):void
		{
			GCPlayState.enableScreen();
		}
	}
}